package net.lainiao.service.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lainiao.base.common.ModelCommon;
import net.lainiao.base.model.Account;
import net.lainiao.service.dao.AccountMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/1/5.
 */
@Service
public class AccountService {
    @Resource
    private AccountMapper accountMapper;

    public PageInfo<Account> getAllForPage(int pageIndex,int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<Account>  list=accountMapper.selectAll();
        PageInfo<Account> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    public int save(Account account){
        if(StringUtils.isEmpty(account.getId())){
            account.setId(ModelCommon.getNewID());
            int count=accountMapper.insert(account);
            return count;
        }
        else{
            return accountMapper.updateByPrimaryKey(account);
        }
    }

    public Account getById(String key){
        return accountMapper.selectByPrimaryKey(key);
    }

    public int del(String key){
        return accountMapper.deleteByPrimaryKey(key);
    }

}
